package com.example.timernote

import android.content.Context
import android.content.SharedPreferences
import android.os.Parcel
import android.util.Log
import com.alibaba.fastjson.JSON

object BloodPressureInfoService{

    val LOG_TAG = "service"
    val NOTE_NAME = "bloodPressureInfoNote"
    val NOTE_KEY = "BP_KEY"

    val STD_LOW = "STD_LOW"
    val STD_LOW_DEFAULT = 90

    val STD_HIGH = "STD_HIGH"
    val STD_HIGH_DEFAULT = 130

    val STD_RATE = "STD_RATE"
    val STD_RATE_DEFAULT = 80



    var sharedPreferences:SharedPreferences
    lateinit var list:MutableList<BloodPressureInfo>

    init {
        sharedPreferences = MainApplication.getContext().getSharedPreferences(NOTE_NAME,Context.MODE_PRIVATE)
    }

    fun selectAll():MutableList<BloodPressureInfo>{
        list = mutableListOf<BloodPressureInfo>()

        var strList = sharedPreferences.getString(NOTE_KEY,"")
        Log.d(LOG_TAG,".get="+strList)

        if (strList.isNotEmpty()){
            list = JSON.parseArray(strList,BloodPressureInfo::class.java)
        }
        return list
    }

    fun add(bloodPressureInfo: BloodPressureInfo){
        list.add(bloodPressureInfo)
        var listStr = JSON.toJSONString(list)
        var editor = sharedPreferences.edit()
        Log.d(LOG_TAG,".add="+listStr)
        editor.putString(NOTE_KEY,listStr)
        editor.apply()
    }

    fun standardValue(key: String):Int{
        var defValue = 0
        when (key){
            STD_LOW ->defValue = STD_LOW_DEFAULT
            STD_HIGH ->defValue = STD_HIGH_DEFAULT
            STD_RATE ->defValue = STD_RATE_DEFAULT
        }
        var value = sharedPreferences.getInt(key,defValue)
        Log.d(LOG_TAG,".get"+key+"="+value+",defVal="+defValue)
        return value
    }



}